package com.springweb.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.springweb.simpleform.EmployeeDetails;

public class EmployeeValidator implements Validator {

	@Override
	public boolean supports(Class<?> clazz) {
         System.out.println("inside support method");
		return clazz.getClass().equals(EmployeeDetails.class);
	   
	}
	@Override
	public void validate(Object target, Errors errors) {
		System.out.println("inside validate method... ");
		   
		EmployeeDetails employeeDetails=null;
		
		 if(target instanceof EmployeeDetails)
			  employeeDetails=(EmployeeDetails)target;
		 else 
			 return ;
		 
		 if(employeeDetails.getAge()<18){
			 errors.rejectValue("age", "age.notValid", "Age must be greater than 18 !");
		 }
		 
		  if(null!=employeeDetails.getImage() &&  !employeeDetails.getImage().getContentType().contains("png")){
			     errors.rejectValue("image", "image.png","Only png file is  allowed.");
		  }
		 
	
		
	}

}
